/* eslint-disable eqeqeq */
/* eslint-disable radix */
/* eslint-disable func-style */
const express = require('express');
const router = express.Router();

const types = [
    {
        id: '1',
        level: 1,
        name: '系统'
    },
    {
        id: '2',
        level: 2,
        isLeaf: false,
        name: '总成'
    },
    // {
    //     id: '3',
    //     level: 3,
    //     isLeaf: false,
    //     name: '总成2'
    // },
    {
        id: '4',
        level: 4,
        isLeaf: true,
        name: '配件'
    }
];

function mockData(id) {
    const data = [];
    for(let i = 1; i < 10; i++) {
        const type = types[i % 3];
        data.push({
            id: `${id}-${i}`,
            code: `Code-${id}-${i}`,
            name: `Name-${id}-${i}`,
            typeId: type.id,
            typeName: type.name,
            remark: `Remark${i}`,
            isLeaf: i % 3 === 2 || i % 4 === 0,
            status: i % 2 === 1 ? 1 : -1,
        });
    }
    return data;
}

router.get('/:id/children', (req, res) => {
    const {pageIndex, pageSize} = req.query;
    res.json({
        payload: {
            content: mockData(req.params.id)
        }
    });
});

router.get('/topLevel/forCurrentUser', (req, res) => {
    res.json({
        payload: {
            content: [{
                id: '1',
                code: 'Code-1',
                name: 'Name-1',
                typeId: '1',
                typeName: '系统',
                remark: 'Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1Remark-1',
                status: 1,
                isLeaf: false,
                rowVersion: 'rowVersion',
                options: ['enable', 'disable'],
            }, {
                id: '2',
                code: 'Code-2',
                name: 'Name-2',
                typeId: '1',
                typeName: '系统',
                remark: 'Remark-2',
                status: -1,
                rowVersion: 'rowVersion',
                options: ['enable', 'disable'],
                isLeaf: false
            }, {
                id: '3',
                code: 'Code-3',
                name: 'Name-3',
                typeId: '1',
                typeName: '系统',
                remark: 'Remark-3',
                isLeaf: false,
                status: -1,
                rowVersion: 'rowVersion',
                options: ['enable', 'disable'],
                isCustomized: true
            }, {
                id: '4',
                code: 'Code-4',
                name: 'Name-4',
                typeId: '1',
                typeName: '系统',
                remark: 'Remark-4',
                isLeaf: false,
                status: 1,
                rowVersion: 'rowVersion',
                options: ['enable', 'disable'],
                isCustomized: true
            }, {
                id: '5',
                code: 'Code-5',
                name: 'Name-5',
                typeId: '1',
                typeName: '系统',
                remark: 'Remark-5',
                status: 1,
                rowVersion: 'rowVersion',
                options: ['enable', 'disable'],
                isLeaf: false
            }]
        }
    });
});

const categories = [];
for(let i = 1; i < 200; i++) {
    const type = types[i % 3];
    categories.push({
        id: `${i}`,
        code: `Code-${i}`,
        name: `Name-${i}`,
        typeId: type.id,
        typeName: type.name,
        status: i % 2 || -1,
        remark: `Remark${i}`,
        options: i % 2 ? ['update', 'disable'] : ['enable'],
        rowVersion: `rowVersion-${i}`,
    });
}


router.get('/forCurrentUser', (req, res) => {
    const {pageIndex, pageSize, status} = req.query;
    const result = status === null || status === undefined ? categories : categories.filter(item => item.status == status);
    res.json({
        payload: {
            content: result.slice(parseInt(pageIndex) * parseInt(pageSize), (parseInt(pageIndex) + 1) * parseInt(pageSize)),
            pageIndex,
            pageSize,
            totalElements: result.length
        }
    });
});

router.get('/:id/ancestors', (req, res) => {
    res.json({
        payload: {
            id: req.params.id,
            code: `Code-${req.params.id}`,
            name: `Name-${req.params.id}`,
            typeId: req.params.id,
            typeName: `Type-${req.params.id}`,
            ancestors: [{
                id: `P1-Id-${req.params.id}`,
                code: `P1-Code-${req.params.id}`,
                name: `P1-Name-${req.params.id}`,
                typeId: `P1-Id-${req.params.id}`,
                typeName: `P1-Type-${req.params.id}`,
            }, {
                id: `P2-Id-${req.params.id}`,
                code: `P2-Code-${req.params.id}`,
                name: `P2-Name-${req.params.id}`,
                typeId: `P2-Id-${req.params.id}`,
                typeName: `P2-Type-${req.params.id}`,
            }, {
                id: `P3-Id-${req.params.id}`,
                code: `P3-Code-${req.params.id}`,
                name: `P3-Name-${req.params.id}`,
                typeId: `P3-Id-${req.params.id}`,
                typeName: `P3-Type-${req.params.id}`,
            }]
        }
    });
});


/**
 * @param {Object} options
 * @throws {Error}
 * @return {Promise}
 */
router.post('/', async(req, res, next) => {
    const options = {
    };
    const data = {
        message: 'message',
        payload: {
            id: 'id1722',
            options: ['enable', 'disable'],
            rowVersion: 'rowVersion'
        }
    };
    res.json(data);
});
/**
 * @param {Object} options
 * @param {} options.id 维修项目分类Id
 * @throws {Error}
 * @return {Promise}
 */
router.get('/:id/ancestors', async(req, res, next) => {
    const options = {
        id: req.params.id
    };
    const data = {
        message: 'message',
        payload: {
            id: 'id1723',
            code: 'code1724',
            name: 'name1725',
            typeId: 'typeId1726',
            typeName: 'typeName1727',
            ancestors: [
                {
                    id: 'id1728',
                    code: 'code1729',
                    name: 'name1730',
                    typeId: 'typeId1731',
                    typeName: 'typeName1732'
                },
                {
                    id: 'id1733',
                    code: 'code1734',
                    name: 'name1735',
                    typeId: 'typeId1736',
                    typeName: 'typeName1737'
                }
            ]
        }
    };
    res.json(data);
});
/**
 * @param {Object} options
 * @param {} options.id 维修项目分类Id
 * @throws {Error}
 * @return {Promise}
 */
router.put('/:id', async(req, res, next) => {
    const options = {
        id: req.params.id
    };
    const data = {
        message: 'message',
        payload: {
            options: ['enable', 'disable'],
            rowVersion: 'rowVersion'
        }
    };
    res.json(data);
});
/**
 * @param {Object} options
 * @param {} options.id 维修项目分类Id
 * @throws {Error}
 * @return {Promise}
 */
router.put('/:id/disable', async(req, res, next) => {
    const options = {
        id: req.params.id
    };
    const data = {
        message: 'message',
        payload: {
            options: ['enable', 'disable'],
            rowVersion: 'rowVersion'
        }
    };
    res.json(data);
});
/**
 * @param {Object} options
 * @param {} options.id 维修项目分类Id
 * @throws {Error}
 * @return {Promise}
 */
router.put('/:id/enable', async(req, res, next) => {
    const options = {
        id: req.params.id
    };
    const data = {
        message: 'message',
        payload: {
            options: ['enable', 'disable'],
            rowVersion: 'rowVersion'
        }
    };
    res.json(data);
});
/**
 * @param {Object} options
 * @throws {Error}
 * @return {Promise}
 */
router.post('/import/forCurrentUser', async(req, res, next) => {
    const options = {
    };
    const data = {
        message: 'message'
    };
    res.json(data);
});

module.exports = router;
